package cn.itamt.utils.grouploader {
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;		

	/**
	 * 多个文件的加载.
	 * @author tamt
	 */
	public class GroupPreloader extends EventDispatcher {
		public static const START : String = 'start';
		public static const Item_OPEN : String = 'item open';

		private var _items : Array;

		private var _loader : Loader;
		private var _urlLoader : URLLoader;

		public function GroupPreloader() {
		}

		private var _curItemIndex : int;

		public function get curItemIndex() : int {
			return _curItemIndex;
		}

		private var _itemsNum : int;

		public function get itemsNum() : int {
			return _itemsNum;
		}

		/**
		 * 添加一个加载项
		 * 
		 */
		public function addItem(item : GroupPreloadItem) : void {
			if(_items == null)_items = [];
			_items.push(item);
			
			_itemsNum = _items.length;
		}

		/**
		 * 计算所有文件的大小
		 */
		public function calculateFilesSize() : void {
		}

		/**
		 * 开始加载
		 */
		public function start() : void {
			if(_items && _items.length)loadNextItem();
		}

		private var _curItem : GroupPreloadItem;

		public function get curItem() : GroupPreloadItem {
			return _curItem;
		}

		private function loadNextItem() : void {
			if(_items.length == 0)return;
			_curItemIndex++;
			_curItem = _items.shift();
			if(_curItem.type == GroupPreloadItem.TYPE_SWF || _curItem.type == GroupPreloadItem.TYPE_IMG) {
				if(_loader == null) {
					_loader = new Loader();
					configureLoaderListeners(_loader.contentLoaderInfo);
				}
				
				_loader.load(new URLRequest(_curItem.url));
			} else {
				if(_urlLoader == null) {
					_urlLoader = new URLLoader();
					configureLoaderListeners(_urlLoader);
				}
				
				_urlLoader.load(new URLRequest(_curItem.url));
			}
		}

		private function configureLoaderListeners(ep : EventDispatcher) : void {
			ep.addEventListener(Event.OPEN, onItemOpen);
			ep.addEventListener(Event.COMPLETE, onItemComplete);
			ep.addEventListener(ProgressEvent.PROGRESS, onItemLoading);
			ep.addEventListener(IOErrorEvent.IO_ERROR, onItemIOError);
			ep.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onItemSecurityError);
		}

		private function onItemOpen(evt : Event) : void {
			dispatchEvent(new Event(Item_OPEN));
		}

		private function onItemComplete(evt : Event) : void {
			if(_items.length == 0) {
				dispatchEvent(new Event(Event.COMPLETE));
			} else {
				this.loadNextItem();
			}
		}

		private function onItemLoading(evt : ProgressEvent) : void {
			dispatchEvent(evt);
		}

		private function onItemIOError(evt : IOErrorEvent) : void {
			if(_items.length == 0) {
				dispatchEvent(new Event(Event.COMPLETE));
			} else {
				this.loadNextItem();
			}
		}

		private function onItemSecurityError(evt : SecurityErrorEvent) : void {
			if(_items.length == 0) {
				dispatchEvent(new Event(Event.COMPLETE));
			} else {
				this.loadNextItem();
			}
		}
	}
}
